package com.zeratul.demo.controller;

import com.zeratul.common.core.result.R;
import com.zeratul.demo.domain.Employee;
import com.zeratul.demo.domain.vo.EmployeeVo;
import com.zeratul.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 职工管理控制器
 */
@RestController
@RequestMapping("/api/emp")
public class EmployeeController {

    private final EmployeeService employeeService;
    
    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /**
     * 创建或更新职工信息
     * @param employee 职工信息
     * @return 保存后的职工信息
     */
    @PostMapping
    public R<Employee> save(@RequestBody Employee employee) {
        employeeService.saveOrUpdate(employee);
        return R.success(employee);
    }
    
    /**
     * 根据ID获取职工信息
     * @param id 职工ID
     * @return 职工信息
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable Integer id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
    
    /**
     * 获取所有职工信息
     * @return 职工列表
     */
    @GetMapping("/list")
    public R<List<EmployeeVo>> findAll() {
        List<EmployeeVo> employees = employeeService.selectVoList();
        return R.success(employees);
    }
    
    /**
     * 根据ID删除职工
     * @param id 职工ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public R<Void> deleteById(@PathVariable Integer id) {
        employeeService.removeById(id);
        return R.success();
    }
}